package com.kfm.pm;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateExample {

    public static void main(String[] args) throws ParseException {

        Date date = new Date();
        System.out.println(date);

        // 1970 1 1 00:00:00  1969 11:59:58
        Date date2 = new Date(-2000);
        System.out.println(date2);

        System.out.println(date.getTime());
        // 当前年份 - 1900
        System.out.println(date.getYear());
        // 0 - 11  + 1
        System.out.println(date.getMonth());
        // 1-31 / 30/ 28/ 29
        System.out.println(date.getDate());
        // 0-24
        System.out.println(date.getHours());
        // 00 - 59
        System.out.println(date.getMinutes());
        // 00 - 59
        System.out.println(date.getSeconds());
        // 1993-9-9

        date.setTime(2000);
        System.out.println(date);
        // 1900 + 2023
        date.setYear(2023);
        System.out.println(date);
        Date birth = new Date();
        birth.setYear(1993 - 1900);
        // 1-12   参数范围：0-11  11 12
        birth.setMonth(8);
        birth.setDate(9);
        System.out.println(birth);

        System.out.println(date.equals(birth));
        // 前大后小 1 前小后大 -1
        System.out.println(date.compareTo(birth));
        System.out.println(birth.compareTo(date));
        // 2023-08-25 14:55:00
        date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        // Date 对象 --> String
        String format = sdf.format(date);
        String dateStr = "2025/07/09 14:55:36";
        // String --> Date 对象 String日期时间字符串应该和sdf对象的格式一样
        Date parse = sdf.parse(dateStr);
        System.out.println(parse);
        System.out.println(format);
        System.out.println(sdf.toPattern());
        // 设置指定的格式化格式
        sdf.applyPattern("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(date));
        // 获取当前sdf的格式化模式
        String s = sdf.toPattern();
        System.out.println(s);


    }
}
